<?php

/**
 * 上传多图
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

class CropperImage extends Field
{
    public static $html = <<<EOF
    <div class="layui-form-item ">
    <label for="[FIELD_NAME]"    class="layui-form-label [REQUIRED]">[FORM_NAME]</label>
    <div class="layui-input-inline">
        <img src="{\$data.[FIELD_NAME]|default=''}" id="srcimg[FIELD_NAME]" {if !isset(\$data.[FIELD_NAME]) || \$data.[FIELD_NAME] == ''} style="display: none;"  {/if}  class="layui-upload-img  srcimgavatar">
        <input type="hidden" name="[FIELD_NAME]" id="inputimg[FIELD_NAME]" lay-verify="[FORM_VALIDATE]" value="{\$data.[FIELD_NAME]|default=''}" >
        <div class="layui-input-inline layui-btn-container" style="width: auto;">
            <div class="layui-btn" id="editimg[FIELD_NAME]">请上传[FORM_NAME]</div>
        </div>
       
    </div>
</div>

<script>
        layui.use(['croppers'], function () {
            var croppers = layui.croppers;
        croppers.render({
            elem: '#editimg[FIELD_NAME]'
            , saveW: 555     
            , saveH: 355
            , mark: 555 / 355    
            , area: ['80%','80%'] 
            , url: "{:url('upfile/upload')}"  
            , done: function (data) { 
                $("#inputimg[FIELD_NAME]").val(data.url);
                $("#srcimg[FIELD_NAME]").attr('src', data.url).show();
            }
        });

    });
  
 
</script>\n
EOF;

public static $rules = [
    'required'   => '非空',
    'file_size'  => '文件大小限制',
    'file_image' => '图片类型',
    'regular'    => '自定义正则'
];


    public static function create($data)
    {
        self::getValidateHtml($data['form_validate']);

        $html = self::$html;
        $html = str_replace(['[FORM_NAME]','[FIELD_NAME]', '[FORM_VALIDATE]', '[REQUIRED]'], [$data['form_name'],$data['field_name'], self::$verify, self::$required], $html);
        return $html;
    }
}